﻿/// -------页面描述：系统日志业务逻辑类
/// -------创建日期：2010年05月20日 
/// -------创建人：wang
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace webnew.BLL
{
    public class Sys_Log:System.Web.UI.Page

    {
        private DAL.Sys_LogHelper dal = new webnew.DAL.Sys_LogHelper();
        public Sys_Log() { }

        //添加一条系统日志
        public int Add(Model.Sys_LogInfo model)
        {
            return dal.Add(model);
        }

        public Model.Sys_LogInfo GetList(string id)
        {
            return dal.GetModel(id);
        }

        //获取用户IP地址
        public string GetUserIP(HttpRequest request)
        {
            string x = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            string y = request.ServerVariables["REMOTE_ADDR"];
            if (string.IsNullOrEmpty(x))
            {
                return y;
            }
            else
            {
                return x;
            }
        }

        /// <summary>
        /// 取得数据行数,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public int GetCount(string strTable, string condition)
        {
            return dal.GetCount(strTable, condition);
        }

        /// <summary>
        /// 分页获取泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<webnew.Model.Sys_LogInfo> GetPageList(string field,string condition, int pageSize, int pageIndex, string tbNames, string sortNames, bool sortType)
        {
            if (pageSize <= 0)
                throw new Exception("每页数据条数必须大于0。");

            if (pageIndex <= 0)
                throw new Exception("页索引必须大于0。");

            if (string.IsNullOrEmpty(sortNames))
                throw new Exception("排序字段不能为空。如果是默认排序，可以填写主键字段。");
            return dal.GetPageList(field,condition, pageSize, pageIndex, tbNames, sortNames, sortType);
        }


        /// <summary>
        /// 根据用户表ID获取用户姓名
        /// </summary>
        /// <returns></returns>
        public string GetTrueName(string userID)
        {
            return dal.GetTrueName(userID);
        }
        /// <summary>
        /// 根据用户表ID获取用户ID
        /// </summary>
        /// <returns></returns>
        public string GetLoginID(string userID)
        {
            return dal.GetLoginID(userID);
        }

        ///// <summary>
        ///// 获取新序号
        ///// </summary>
        ///// <returns></returns>
        //public int GetNewSort()
        //{
        //     return dal.GetMaxSort()+1;
        //}

        /// <summary>
        /// 添加一条系统日志
        /// </summary>
        /// <param name="error">错误信息</param>
        /// <param name="operateLag">操作标志:"添加","修改","删除"</param>
        /// <param name="modelLog">系统日志model</param>
        /// <param name="operatePram">操作参数</param>
        /// <param name="blogContentPram">备注参数</param>
        public void AddSysLog(string error, string operateLag, Model.Sys_LogInfo modelLog, string operatePram, string blogContentPram)
        {
            //----------正常
            if (error == "")
            {
                //登陆
                if (operateLag == "登录")
                {
                    modelLog.Operate = "登入系统";           //操作
                    modelLog.Status = "正常";                    //状态
                    modelLog.BlogContent = "";
                }
                
                //添加
                if (operateLag == "添加")
                {
                    modelLog.Operate = "添加" + operatePram;           //操作
                    modelLog.Status = "正常";                    //状态
                    modelLog.BlogContent = "添加"+blogContentPram;     //备注
                }
                //修改
                if (operateLag == "修改")
                {
                    modelLog.Operate = "修改" + operatePram;           //操作
                    modelLog.Status = "正常";                    //状态
                    modelLog.BlogContent = "修改"+blogContentPram; ;     //备注
                }
                //删除
                if (operateLag == "删除")
                {
                    modelLog.Operate = "删除" + operatePram;           //操作
                    modelLog.Status = "正常";                    //状态
                    modelLog.BlogContent = "删除" + blogContentPram;      //备注
 
                }
            }
            
            //-----------出错
            else
            {
                //登陆
                if (operateLag == "登录")
                {
                    modelLog.Operate = "登入系统";           //操作
                    modelLog.Status = "出错";                    //状态
                    modelLog.BlogContent = error.ToString();     //备注
                }
                //添加
                if (operateLag == "添加")
                {
                    modelLog.Operate = "添加"+operatePram;           //操作
                    modelLog.Status = "出错";                    //状态
                    modelLog.BlogContent = error.ToString();     //备注
                }
                //修改
                if (operateLag == "修改")
                {
                    modelLog.Operate = "修改" + operatePram;           //操作
                    modelLog.Status = "出错";                    //状态
                    modelLog.BlogContent = error.ToString();     //备注
                }
                //删除
                if (operateLag == "删除")
                {
                    modelLog.Operate = "删除" + operatePram;           //操作
                    modelLog.Status = "出错";                    //状态
                    modelLog.BlogContent = error.ToString();     //备注
                }
            }

            //添加一条系统日志
            Add(modelLog);
              
        }

    }
}
